// maplit = "1.0.2"
// reqwest = { version = "0.12.22", features = ["json","cookie"] }
// serde = { version = "1.0.219", features = ["derive"] }
// serde_json = "1.0.141"
// tokio = { version = "1.46.1", features = ["full"] }
// url = "2.5.4"
use maplit::hashmap;
use reqwest;
use reqwest::cookie::Jar;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc;
use url::Url;

#[derive(Debug, Clone, Deserialize)]
pub struct UaList {
    pub data: Vec<String>,
}

pub async fn GetUa(page: String, pt: String) -> Result<UaList, Box<dyn std::error::Error>> {
    let url = "https://www.bejson.com/Bejson/Api/Common/ge_nua";
    let raw_headers = hashmap! {
        "Accept"=> "application/json, text/javascript, */*; q=0.01",
        "Accept-Language"=> "en,zh-CN;q=0.9,zh;q=0.8",
        "Cache-Control"=> "no-cache",
        "Connection"=> "keep-alive",
        "Content-Type"=> "multipart/form-data; boundary=----WebKitFormBoundaryQIfyCxtDFznDEYQB",
        "Origin"=> "https://www.bejson.com",
        "Pragma"=> "no-cache",
        "Referer"=> "https://www.bejson.com/network/useragent_rnd/",
        "Sec-Fetch-Dest"=> "empty",
        "Sec-Fetch-Mode"=> "cors",
        "Sec-Fetch-Site"=> "same-origin",
        "User-Agent"=> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
        "X-Requested-With"=> "XMLHttpRequest",
        "sec-ch-ua"=> "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"",
        "sec-ch-ua-mobile"=> "?0",
        "sec-ch-ua-platform"=> "\"macOS\"",
    };

    let mut headers = HeaderMap::new();
    for (k, v) in raw_headers {
        headers.insert(
            HeaderName::from_bytes(k.as_bytes())?,
            HeaderValue::from_str(v)?,
        );
    }
    let cookies = hashmap! {
            "Hm_lvt_4aa52dbe1c3f25aa133d68ee023b1c61"=> "1741023217",
            "bejson_saved_qqlist"=> "5d%2000%2000%2001%2000%2009%2004%2000%2000%2000%2000%2000%2000%2000%202d%209e%0Ac0%2046%2093%205a%20ce%2034%20f2%201e%209f%20e9%20fa%2080%20b6%20ce%2003%20ff%0A4e%2087%2012%208d%20e8%2025%205e%20ad%2018%2021%20a9%2013%20b3%20ca%2068%20a0%0A1c%20df%20b7%2094%2004%20fa%2080%20c0%20fb%205b%204c%2077%2013%20ab%206a%20f6%0A45%20ad%20eb%2061%203e%2087%201b%2034%2049%20b2%20d5%20d9%2045%202b%2083%2069%0Aca%20ae%2090%20cd%204d%20de%2057%2061%20a2%209f%2017%208c%20a3%2025%2010%2035%0A5c%20e9%2017%2098%202c%2002%20d6%20c6%20e6%209b%2087%2076%20ec%201c%20f8%20dc%0A07%2035%206a%20e5%20f1%20bb%20b9%2065%204a%2070%20b8%2085%20f6%20e2%2055%20fe%0A9b%2013%20b6%20ae%20e1%203a%202b%20b8%2074%209f%2013%20c7%20bf%20b2%202e%2040%0A6e%204c%2058%201e%2078%20bb%20cd%208d%2012%2086%202e%2074%2072%2091%2050%2037%0Ad3%2082%20c6%200c%20ba%20b3%201c%206e%203d%2027%204a%2024%201f%20e0%2051%2049%0A28%20c8%20ec%2024%209d%2058%20cb%207d%20f3%20da%206b%204c%209e%2014%203b%2004%0Ab2%2000%204e%20f1%2099%20f7%206a%20f0%20ef%2031%202b%2050%20a9%2048%20c5%2083%0A2d%202c%2067%2067%2055%20f4%20a8%20b1%20e5%208e%2074%20f2%20f5%2059%20d2%2056%0A9a%206a%20e6%20d4%206e%20d0%208d%2017%20a7%20f7%207c%2029%2057%2095%2024%20d5%0A66%20ad%20fc%2004%202f%20b6%2022%20e1%2072%20c1%20ba%2039%20eb%20b5%20f9%20fc%0A5f%20a8%2086%2073%20f4%202c%20cc%20d4%202f%2069%20bc%20af%2001%2096%2045%2019%0A14%2030%2035%20a4%2023%200c%209a%2001%208e%2043%20b4%20ee%20bd%2048%2098%2087%0A0c%20be%20f8%207b%20bf%20c5%204a%2086%203b%2085%20e8%205d%2037%208f%208a%206c%0Af3%2037%20e5%20da%202b%2085%208f%2072%2030%2070%20f9%2003%20b7%20c6%20d6%206d%0A02%20f8%2047%20a7%206b%20a3%2091%20f6%20db%203f%2092%207f%2095%20e7%207f%2078%0Aad%20a1%2068%20fb%2087%20c5%2092%20c3%2007%204d%209a%20a6%2006%20c0%20fa%203c%0A35%2039%20e8%2000%20bb%2064%2030%20f1%2063%2079%20e2%20c0%204b%20ef%201e%207e%0A93%2088%200c%20bd%2057%204f%20b1%20e3%20eb%2023%2080%206b%207d%20ba%2092%2068%0Ad6%206c%20d9%209a%20a6%2014%206e%20ec%20f6%2005%20fd%206c%20b5%2098%2025%20fc%0A35%20b5%202c%2080%20e3%201f%2019%209d%20d6%206b%2072%20fc%2072%2057%20af"
    };
    let form = reqwest::multipart::Form::new()
        .text("count", page)
        .text("type", pt);

    let jar = Jar::default();
    let url_parsed = Url::parse(url)?;
    for (k, v) in &cookies {
        jar.add_cookie_str(&format!("{k}={v}"), &url_parsed);
    }
    let client = reqwest::Client::builder()
        .cookie_provider(Arc::new(jar))
        .default_headers(headers)
        .build()?;
    let response = client
        .request(reqwest::Method::POST, url)
        .multipart(form)
        .send()
        .await?;

    let text = &response.text().await?;
    let ua_list: UaList = serde_json::from_str(text)?;

    Ok(ua_list)
}
